/**
 * @Project Name:hoofungson-elk-log
 * @Package Name:cn.hoofungson.framework.elk.log.converter.pattern
 * @Since JDK 1.8
 */

package cn.hoofungson.framework.elk.log.converter.pattern;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import cn.hoofungson.framework.elk.log.constant.config.MonitorLogFieldConstants;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: MonitorCodePatternConverter
 * @Author 胡松 hoofungson@163.com
 * @Date 2020-03-07 18:28
 * @Version V1.0
 */
public class MonitorCodePatternConverter extends ClassicConverter {

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        if (StringUtils.contains(iLoggingEvent.getMessage(), MonitorLogFieldConstants.MONITOR_CODE)) {
            //此时日志进入监控日志处理逻辑
            JSONObject jsonObject = JSONUtil.parseObj(iLoggingEvent.getMessage());
            if (null != jsonObject) {
                return StringUtils.isNotBlank(jsonObject.getStr(MonitorLogFieldConstants.MONITOR_CODE)) ? jsonObject.getStr(MonitorLogFieldConstants.MONITOR_CODE) : StringUtils.EMPTY;
            }
        } else {
            return StringUtils.EMPTY;
        }
        return StringUtils.EMPTY;
    }
}
